package com.jia.leetCode;

/**
 * @program: Leetcode
 * @description: https://leetcode-cn.com/problems/gas-station/submissions/
 * @author: STU756
 * @create: 2020-09-04 11:33
 */
public class Pro0134 {
    public int canCompleteCircuit(int[] gas, int[] cost) {
        int gasSum = 0;
        for(int i = 0; i < gas.length; i++) {
            gasSum = 0;
            if(gas[i] >= cost[i]) {
                int j = i;
                do{
                    gasSum += gas[j] - cost[j];
                    if(gasSum < 0) break;
                    j++;
                    if(j == gas.length) {
                        j = 0;
                    }
                }while(j != i);
                if(i == j) return i;
            }
        }
        return -1;
    }
}
